<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/9/24
 * Time: 14:13
 */

namespace app\admin\controller;


use app\admin\model\RoomModel;
use app\admin\model\SchoolModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;

class RoomController extends AdminBaseController
{
    public function index()
    {
        $name = $this->request->param('name');
        $school_id = $this->request->param('school_id');
        $school=Db::name('school')->select();
        $data = Db::name('room')
            ->where(function (Query $query) use($name,$school_id){
                if ($name){
                    $query->where('ji_room.name','like',"%$name%");
                }
                if ($school_id) {
                    $query->where('school_id', 'eq', $school_id );
                }
            })
            ->join('ji_school','ji_school.id = ji_room.school_id','LEFT')
            ->join('ji_student','ji_student.room_id = ji_room.id','LEFT')
            ->join('ji_record','ji_record.student_id=ji_student.id','LEFT')
            ->join('ji_score','ji_score.id=ji_record.score_id','LEFT')
            ->field('ji_room.*')
            ->field('ji_school.name as school_name ')
            ->field('sum(ji_score.name) as total')
            ->group('ji_room.id')
            ->order('id desc')
            ->paginate(3,false,$this->request->param());
        $data->appends(['name'=>$name,'school_id'=>$school_id]);
        //分页样式
        $page = $data->render();
        //渲染显示页面，接收返回的值
        return view('',['room'=>$data,'page'=>$page, 'school_id'=>$school_id,'school'=>$school]);
    }
    public function add()
    {
        $school=SchoolModel::select();
        return $this->fetch('',['data'=>$school]);
    }
    public function addPost()
    {
        $data=input();
        if (empty($data['name'])){
            $this->error('班级名不能为空');
        }
        $add=RoomModel::create($data);
        if ($add){
            $this->success('添加成功','room/index');
        }else{
            $this->error('添加失败');
        }
    }
    public function edit(){
        $data=input();
        $room=RoomModel::get($data['id']);
        $school=SchoolModel::select();
        return $this->fetch('',['room'=>$room,'school'=>$school]);
    }
    public function editPost()
    {
        $data = input();
        if (empty($data['name'])){
            $this->error('班级名不能为空');
        }
        $edit = RoomModel::update($data,['id'=>$data['id']]);
        if ($edit){
            $this->success('修改成功','room/index');
        }else{
            $this->error('修改失败');
        }
    }
    public function detail()
    {
        $id=input('id');
        $room = Db::name('room')
            ->alias('r')
            ->join('ji_student stu','stu.room_id = r.id')
            ->join('ji_record re','re.student_id=stu.id','LEFT')
            ->join('ji_score s','s.id=re.score_id','LEFT')
            ->join('ji_reason rea','rea.id = re.reason_id')
            ->field('r.*')
            ->field('r.name room_name,rea.name reason_name,stu.name student_name,s.name score_name')
            ->where(['r.id'=>$id])
            ->select();
        return $this->fetch('',['room'=>$room]);
    }
    public function delete()
    {
        $id=input('id');
        $del=RoomModel::destroy($id);
        if ($del){
            $this->success("删除成功！", url("room/index"));
        }else{
            $this->error("删除失败！");
        }
    }
    public function delAll()
    {
        $ids = input('ids/a');
        $del = RoomModel::destroy($ids);
        if($del){
            $this->success("删除成功！", url("room/index"));
        }else{
            $this->error("删除失败！");
        }
    }
}